<?php
/*****************************************************************
 * Fusion.Board
 *
 * @author                Denis Wrobel
 * @license               http://www.gnu.org/licenses/gpl-3.0.html
 * @version               0.1
 *
 *****************************************************************/

if( !defined( 'IN_SCRIPT' ) )
{
    die( 'Go Away!' );
}

class DataBase
{
    private $connection = NULL;
    public $queries = array();
    public $queries_num = 0;
    private $last_query;
    public $quering_time = 0;
    
    /*
     * Constructor, connect to mysql and select db
     * @param array $connect_data Array with connect information
     *  [server] => DB Server
     *  [user]   => DB User
     *  [pass]   => DB Password
     *  [name]   => DB Name
     *
     * @return bool
     */
    public function __construct( $connect_data )
    {
        $quering_time = microtime( true );
        
        //Connect to mysql server and select database
        $this->connection = @mysql_connect( $connect_data['server'], $connect_data['user'], $connect_data['pass'] );
        $db_selection     = @mysql_select_db( $connect_data['name'], $this->connection );
        
        $this->quering_time += microtime( true ) - $quering_time;
        
        //Check, if connection is ok
        if( !$this->connection || !$db_selection )
        {
            //Connection failure
            return false;
        }
        else
        {
            //Connection ok
            mysql_set_charset( 'utf8', $this->connection );
            return true;
        }
    }
    
    /*
     * Execute SQL Query
     * @param string $sql SQL Query
     * @return query
     */
    public function query( $sql )
    {
        global $error;
        
        if( $sql )
        {
            $quering_time = microtime( true );
            
            $query = @mysql_query( $sql );
            
            $this->quering_time += microtime( true ) - $quering_time;
            
            $this->last_query = $query;
            
            $this->queries_num++;
            
            if( mysql_error() )
            {
                $error->scriptError( mysql_error() );
            }
            
            return $query;
        }
    }
    
    
    /*
     * Does the same as mysql_fecth_assoc
     */
    public function fetch( $query )
    {
        if( $query )
        {
            $quering_time = microtime( true );
            
            $fetch = mysql_fetch_assoc( $query );
            
            $this->quering_time += microtime( true ) - $quering_time;
            
            return $fetch;
        }
    }
    
    /*
     * Does the same as mysql_fecth_array
     */
    public function fetch_array( $query )
    {
        if( $query )
        {
            return mysql_fetch_array( $query );
        }
    }
    
    /*
     * Free result
     */
    public function freeResult()
    {
        if( isset( $this->last_query ) )
        {
            $quering_time = microtime( true );
            
            mysql_free_result( $this->last_query );
            
            $this->quering_time += microtime( true ) - $quering_time;
        }
    }
    
    /**
     * Destructor closses DB connection
     */
	
    public function __destruct()
    {
        // Close connection if we are connected
        if( $this->connection )
        {
            $quering_time = microtime( true );
            
            mysql_close( $this->connection );
            
            $this->quering_time += microtime( true ) - $quering_time;
        }
    }
    
    public function escape( $string = '' )
    {
        return addslashes( $string );
    }
}


?>